home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-12-14 | 4.5 KB | 193 lines |
- import java.awt.*;
-
- class ScrollText extends Canvas implements Runnable
- {
- Thread scrollThread = null;
- String text; //text to scroll
- int scrollUnit = 10; //the incremental scrolling distance in pixels
- int sleepTime = 150; //controls the speed of the scroll
-
- public static final boolean SCROLL_LEFT = true;
- public static final boolean SCROLL_RIGHT = false;
-
- boolean scrollDirection = SCROLL_LEFT;
-
- private boolean suspended = false;
-
- private int textX;
- private int textY;
- private int textW;
-
- public ScrollText()
- {
- super();
- setText("Visual Cafe from Symantec");
- setBackground(Color.lightGray);
- setForeground(Color.white);
- }
-
- public synchronized void addNotify()
- {
- super.addNotify();
- scrollThread = new Thread(this);
- scrollThread.setPriority(Thread.MIN_PRIORITY);
- scrollThread.start();
- }
-
- public synchronized void removeNotify() {
- if (scrollThread != null) {
- scrollThread.stop();
- scrollThread = null;
- }
- super.removeNotify();
- }
-
- public void startScrollText() {
- suspended = false;
- show();
- }
-
- public void stopScrollText() {
- suspended = true;
- }
-
- public void setText(String str)
- {
- text = str;
- createTextParams();
- }
-
- public String getText()
- {
- return text;
- }
-
- public void setScrollInterval(int speed)
- {
- if (speed < 1)
- sleepTime = 1;
- else
- sleepTime = speed;
- }
-
- public int getScrollInterval()
- {
- return sleepTime;
- }
-
- public void setScrollUnit(int unit)
- {
- if (unit < 1)
- scrollUnit = 1;
- else
- scrollUnit = unit;
- }
-
- public int getScrollUnit()
- {
- return (scrollUnit < 0) ? -scrollUnit : scrollUnit;
- }
-
- public void setScrollDirection(boolean dir)
- {
- scrollDirection = dir;
- }
-
- public boolean getScrollDirection()
- {
- return scrollDirection;
- }
-
- public synchronized void show() {
- super.show();
- if (isVisible()) {
- if (scrollThread != null) {
- scrollThread.setPriority(Thread.MAX_PRIORITY);
- scrollThread.resume();
- }
- }
- }
-
- public synchronized void hide() {
- super.hide();
- if (!isVisible()) {
- if (scrollThread != null)
- scrollThread.suspend();
- }
- }
-
- public void run()
- {
- createTextParams();
- while (true)
- {
- if (!suspended)
- {
- nextPos();
- try
- {
- System.gc();
- Thread.sleep(sleepTime);
- }
- catch(Exception e)
- {
- }
- }
- }
- }
-
- public synchronized void nextPos()
- {
- Dimension dim = size();
- if (scrollDirection == SCROLL_LEFT)
- {
- textX -= scrollUnit; //scrollUnit is the incremental distance of scrolling
- if((textX + textW) < 0)
- textX = dim.width;
- }
- else
- {
- textX += scrollUnit;
- if (textX > dim.width)
- textX = -textW;
- }
-
- repaint();
- }
-
- private void createTextParams()
- {
- Font f = getFont();
- if (f == null)
- return;
- FontMetrics fm = getFontMetrics(f);
- int fh = fm.getHeight();
-
- Dimension dim = size();
- textX = dim.width;
- textY = ((dim.height - fh) >> 1) + fm.getAscent();
- textW = fm.stringWidth(text);
- }
-
- public void paint(Graphics g)
- {
- Dimension dim = size();
- Image textImage = createImage(dim.width, dim.height);
- Graphics textGC = textImage.getGraphics();
-
- //draw background rectangle...
- textGC.setColor(getBackground());
- textGC.fillRect(0, 0, dim.width-1, dim.height-1);
-
- //...then draw shadow copy of text...
- textGC.setColor(Color.black);
- textGC.drawString(text, textX+2, textY+2);
-
- //...then draw top copy of text...
- textGC.setColor(getForeground());
- textGC.drawString(text, textX, textY);
- g.drawImage(textImage, 0, 0, this);
- }
- }
-
-